package models

import (
	"gorm.io/gorm"
	"time"
)

type AdminLog struct {
	ID            uint64    `gorm:"primaryKey;column:id" json:"id"`
	AdminID       int       `gorm:"column:admin_id;not null" json:"admin_id"`
	TableName     string    `gorm:"size:50;not null" json:"table_name"` // 字段名保持不变
	TargetID      uint64    `gorm:"column:target_id;not null" json:"target_id"`
	ChangedFields string    `gorm:"type:text;not null" json:"changed_fields"` // JSON格式
	CreatedAt     time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP" json:"created_at"`
	Admin         Admin     `gorm:"foreignKey:AdminID" json:"-"`
}

func (a *AdminLog) BeforeCreate(tx *gorm.DB) (err error) {
	if a.CreatedAt.IsZero() {
		a.CreatedAt = time.Now()
	}
	return nil
}

// TableNameFunc 指定表名
func (AdminLog) TableNameFunc() string {
	return "admin_log"
}
